/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package java_xpath;


import BUS.*;
import DTO.*;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;


import java.awt.Graphics2D;
import java.awt.GridLayout;

import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Vector;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;

import javax.swing.DefaultComboBoxModel;
import javax.swing.*;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
/**
 *
 * @author phihai
 */
public class GUI extends javax.swing.JFrame {

    /**
     * Creates new form GUI
     */
    
    DefaultComboBoxModel model;
    ArrayList<Topic_DTO> arr_topic;
    ArrayList<NoiDung_DTO> arr_noidung;
    Vector masite ;
    Vector matpoic ;
    Vector matopic_tabhienthi = new Vector();
    
    public GUI() {
        initComponents();   
        LoadWeb();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the GUI Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        cmb_Site = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        cmb_chuyenmuc = new javax.swing.JComboBox();
        btn_themchuyenmuc = new javax.swing.JButton();
        btn_themsite = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txt_weblink = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txt_Xpath_link = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_Xpath_title = new javax.swing.JTextField();
        txt_Xpath_img = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txt_time = new javax.swing.JTextField();
        txt_Xpath_mota = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txt_Xpath_trangtiep = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        cb_tenSite = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        cb_tenTopic = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        btn_laytin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });

        cmb_Site.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmb_Site.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmb_SiteActionPerformed(evt);
            }
        });

        jLabel8.setText("Site");

        jLabel9.setText("Chuyên mục");

        cmb_chuyenmuc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btn_themchuyenmuc.setText("...");
        btn_themchuyenmuc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_themchuyenmucActionPerformed(evt);
            }
        });

        btn_themsite.setText("...");
        btn_themsite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_themsiteActionPerformed(evt);
            }
        });

        jLabel1.setText("WebLink ");

        jLabel2.setText("Xpath_link");

        jLabel3.setText("Xpath_title");

        jLabel4.setText("Xpath_limg");

        jLabel5.setText("Xpath_time");

        txt_time.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_timeActionPerformed(evt);
            }
        });

        jLabel6.setText("Xpath_mota");

        jLabel7.setText("Xpath_trang tiep");

        txt_Xpath_trangtiep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_Xpath_trangtiepActionPerformed(evt);
            }
        });

        jButton2.setText("Lấy tin");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_weblink, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_Xpath_link)
                            .addComponent(txt_Xpath_title)
                            .addComponent(txt_Xpath_img)
                            .addComponent(txt_time)
                            .addComponent(txt_Xpath_mota)
                            .addComponent(txt_Xpath_trangtiep, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cmb_Site, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btn_themsite, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cmb_chuyenmuc, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btn_themchuyenmuc, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(cmb_Site, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btn_themsite, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cmb_chuyenmuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_themchuyenmuc, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_weblink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(txt_Xpath_link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(txt_Xpath_title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txt_Xpath_img, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txt_time, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txt_Xpath_mota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txt_Xpath_trangtiep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(155, 155, 155))
        );

        jTabbedPane1.addTab("Lấy nội dung", jPanel1);

        cb_tenSite.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cb_tenSite.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cb_tenSiteItemStateChanged(evt);
            }
        });
        cb_tenSite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_tenSiteActionPerformed(evt);
            }
        });

        jButton1.setText("Hiển thị");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        cb_tenTopic.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 704, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 373, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel3);

        btn_laytin.setText("Lấy tin");
        btn_laytin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_laytinActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(cb_tenSite, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cb_tenTopic, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(btn_laytin)
                        .addGap(35, 35, 35)
                        .addComponent(jButton1)))
                .addGap(14, 14, 14))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cb_tenTopic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_tenSite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(btn_laytin))
                .addGap(23, 23, 23)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Xem nội dung", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(54, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 735, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 495, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(44, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        try {           
            LoadNoiDung();                      

        } catch (Exception ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void cb_tenSiteItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cb_tenSiteItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == ItemEvent.SELECTED)
        {
            int index = cb_tenSite.getSelectedIndex();
            LoadTopic(index+1);
        }
    }//GEN-LAST:event_cb_tenSiteItemStateChanged

    private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged
        // TODO add your handling code here:
        JTabbedPane tabbedPane = (JTabbedPane) evt.getSource();
        int Index = tabbedPane.getSelectedIndex();
        if(Index == 1)
        {
            LoadWeb();
            LoadTopic(0);
        }
    }//GEN-LAST:event_jTabbedPane1StateChanged

    private void cmb_SiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmb_SiteActionPerformed
        // TODO add your handling code here:

        // combobox site changed

        matpoic = new Vector();

        int ma = (int) masite.get(cmb_Site.getSelectedIndex());

        Topic_BUS tpbus = new Topic_BUS();
        try {
            ArrayList<Topic_DTO> arr = tpbus.DanhSachTopic(ma);
            model = new DefaultComboBoxModel();
            for (int i = 0; i < arr.size(); i++) {
                model.addElement(arr.get(i).getTen());
                matpoic.addElement(arr.get(i).getId());
            }
            cmb_chuyenmuc.setModel(model);

        } catch (Exception ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_cmb_SiteActionPerformed

    private void btn_themchuyenmucActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_themchuyenmucActionPerformed
        // TODO add your handling code here:

        themchuyenmuc themcm = new themchuyenmuc();
        themcm.setVisible(true);
    }//GEN-LAST:event_btn_themchuyenmucActionPerformed

    private void btn_themsiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_themsiteActionPerformed
        // TODO add your handling code here:

        themsite themst = new themsite();
        themst.setVisible(true);
    }//GEN-LAST:event_btn_themsiteActionPerformed

    private void txt_timeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_timeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_timeActionPerformed

    private void txt_Xpath_trangtiepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_Xpath_trangtiepActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_Xpath_trangtiepActionPerformed

    
    public static String phantich_xpath(String Xpath)
    {
        // thay dau / thanh > 
        Xpath = Xpath.replaceAll("/", " > ");
        
        // bo dau [*]
        Xpath = Xpath.replaceAll("\\[1?2?3?4?5?6?7?8?9?[10]?[11]?[12]?[13]?[14]?[15]?[16]?[17]?[18]?[19]?[20]?[21]?[22]?[23]?[24]?[25]?[26]?[27]?[29]?[30]?[31]?[32]?[33]?[34]?[35]?[36]?]?", "");
        
      
        
        return Xpath;
    }
    
    
    private ArrayList<NoiDung_DTO> laytin(String url, String Xpath_link, String Xpath_title, String Xpath_img, String Xpath_time, String Xpath_content, String Xpath_next) 
    {
         ArrayList<NoiDung_DTO> dstin = new ArrayList<>();
         
        org.jsoup.nodes.Document  doc;
        
        
        // getdomain
        
//            int ma_site = (int) masite.get(cmb_Site.getSelectedIndex()) ;
//            String domain = new String();
//            Site_BUS sb = new Site_BUS();
//            try {
//                domain = sb.laydomain(ma_site);
//            } catch (SQLException ex) {
//                Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
//            }
            
              
                    
            String temp = url.replaceAll("http://", "");
            String[] arr_temp = temp.split("/");
            
            String url_new = arr_temp[0];
            
            
        // phan tich Xpath
            if (Xpath_link.isEmpty() == false) {
                Xpath_link = phantich_xpath(Xpath_link);
            }
            
            if (Xpath_title.isEmpty() == false) {
                Xpath_title = phantich_xpath(Xpath_title);
            }
            
            if (Xpath_img.isEmpty() == false) {
                Xpath_img = phantich_xpath(Xpath_img);
            }
            
            if (Xpath_time.isEmpty() == false) {
                Xpath_time = phantich_xpath(Xpath_time);
            }
            
            if (Xpath_content.isEmpty() == false) {
                Xpath_content = phantich_xpath(Xpath_content);
            }
            
            if (Xpath_next.isEmpty() == false) {
                Xpath_next = phantich_xpath(Xpath_next);
            }
            
            // duyet xpath lay tin
            int k=1;
            while (url != null) {
                if (k==5) {
                    return dstin;
                }
                try {
                    doc = Jsoup.connect(url).get();
                    
                                //set up Timer
                               
                    Elements links = new Elements();
                    Elements titles = new Elements();
                    Elements imgs =  new Elements();
                    Elements times = new Elements();
                    Elements contents = new Elements();
                    Elements nexts = new Elements();
                    
                    // lay noi dung theo xpath cua tung phan anh xa 
                    if (Xpath_link.isEmpty() == false) {
                        links = doc.select(Xpath_link);
                    }
                    
                    if (Xpath_title.isEmpty() == false) {
                        titles = doc.select(Xpath_title);
                    }
                    
                    if (titles.size() == 0) {
                        return dstin;
                    }
                    if (Xpath_img.isEmpty() == false) {
                        imgs = doc.select(Xpath_img);
                    }
                    
                    if (Xpath_time.isEmpty() == false) {
                         times = doc.select(Xpath_time);
                    }
                    
                    if (Xpath_content.isEmpty() == false) {
                      contents = doc.select(Xpath_content); 
                    }
                    
                    if (Xpath_next.isEmpty() == false) {
                        nexts = doc.select(Xpath_next);
                    }
                    
                    // lay tung tin, cho vao list
                    
                    for (int i = 0; i < titles.size(); i++) {
                        
                        NoiDung_DTO tintuc_dto = new NoiDung_DTO();
                        System.out.println("k: " + k);
                        
                        // lay link
                        try {
                            if (links.size() > 0) {
                                Element link = links.get(i);
                                tintuc_dto.setlink(link.attr("href"));
                            }
                        } catch (Exception e) {
                        }
                        
                        
                        
                        // lay title

                        try {
                            if (titles.size() > 0) {
                                Element title = titles.get(i);
                                if (title.attr("title").isEmpty() == false) {
                                    
                                    tintuc_dto.setTen(title.attr("title"));
                                    System.out.println("title: " + title.attr("title"));
                                }
                                else
                                {
                                    
                                    tintuc_dto.setTen(title.text());
                                    System.out.println("title: " + title.text());
                                }
                                
                            }
                        } catch (Exception e) {
                        }

                       
                                               
                        // lay image
                        try {
                            if (imgs.size() > 0) {
                            
                            
                                try {
                                     Element img = imgs.get(i);
                                    if (img != null) {
                                        String url_image = img.attr("href");
                                        if (url_image.isEmpty()) {
                                            url_image = img.attr("src");
                                        }


                                        URL url_img;


                                        url_img = new URL(url_image);
                                        BufferedImage image = ImageIO.read(url_img);




                                        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                                        ImageIO.write(image, "jpg", baos);
                                        byte[] hinhanh = baos.toByteArray(); 
                                        baos.close();
                                        tintuc_dto.setHinhanh(hinhanh);
                                    }
                                } catch (Exception ex) {
                                }
                               
                            }
                        } catch (Exception ex) {
                        }
                        
                        
                        
                        try {
                            // lay time
                            if (times.size() > 0) {
                                Element time = times.get(i);
                                tintuc_dto.setThoiGian(time.text());

                            }
                        } catch (Exception ex) {
                        }
                            
                        
                        try {
                            // lay content
                            if (contents.size() > 0) {
                                Element content = contents.get(i);
                                tintuc_dto.setNoiDung(content.text());
                                 System.out.println("content: " + content.text());

                            }


                        } catch (Exception ex) {
                        }
                                                  
                            // add to list
                        dstin.add(tintuc_dto);                      
                        
                    
                                         
                    }
                    
                    // url den trang tiep theo
                    
                        if (titles.size()==0) {
                            url = null;                           
                        }
                        else
                        {
                            k++;
                            int flag = 0;
                            for (Element url_next  : nexts ) {
                        
                                String index = url_next.text();
                                if (index.isEmpty() == false) {
                                    boolean laso ;
                                    Pattern pattern = Pattern.compile("\\d*"); 
                                     Matcher matcher = pattern.matcher(index); 
                                    if (matcher.matches()) { 
                                        laso = true; 
                                    } else { 
                                        laso = false; 
                                    }

                                 if (laso == true) {

                                     int x = Integer.parseInt(index);
                                     if (x == k) {
                                         url =  url_next.attr("href");
                                         if (url.contains("http") == false) {  //url ko chua domain
                                             // them http://domain
                                             String href = url;
                                             url = "http://" + url_new + url;
                                                     
                                         }
                                         flag = 1;
                                         break;
                                     }
                                 }
                                }
                                
                                             
                            }
                            if (flag == 0) {
                              url = null;
                            }
                        }
                
               
            }
                
            catch (IOException ex) {
                Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);

            }
        
                
            
        }
            
        return dstin;   
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        
        
        try {
            
            ArrayList<NoiDung_DTO> dstin = new ArrayList<>();
            
            // get idchuyenmuc
            int idchuyenmuc = (int) matpoic.get(cmb_chuyenmuc.getSelectedIndex());
            
            
            
            // get Xpath
            String url = txt_weblink.getText(); 
            String Xpath_link = txt_Xpath_link.getText();
            String Xpath_title = txt_Xpath_title.getText();
            String Xpath_img = txt_Xpath_img.getText();
            String Xpath_time = txt_time.getText();
            String Xpath_content = txt_Xpath_mota.getText();
            String Xpath_next = txt_Xpath_trangtiep.getText();
            
            
            
             // Luu Xpath

            Topic_BUS tpbus = new Topic_BUS();
            tpbus.capnhat_xpath(idchuyenmuc, Xpath_link, Xpath_title, Xpath_img, Xpath_time, Xpath_content, Xpath_next, txt_weblink.getText());
               
            dstin =  laytin(url, Xpath_link, Xpath_title, Xpath_img, Xpath_time, Xpath_content, Xpath_next);
            
            
            
            // duyet danh sach tu cuoi va insert
               for (int i = dstin.size()-1; i >= 0; i--) {


                   NoiDung_BUS ndbus = new NoiDung_BUS();
                   ndbus.Themtin(idchuyenmuc , dstin.get(i).getlink(), dstin.get(i).getTen(), dstin.get(i).getHinhanh(), dstin.get(i).getThoiGian(), dstin.get(i).getNoiDung());
               }
          
                
                
            JOptionPane.showMessageDialog(null, "lay tin thanh cong");
        } catch (SQLException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage());
            
        }
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void btn_laytinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_laytinActionPerformed
        // TODO add your handling code here:
        
        try {
            ArrayList<NoiDung_DTO> dstin = new ArrayList<>();
            // get idchuyenmuc
            int idchuyenmuc = (int) matopic_tabhienthi.get(cb_tenTopic.getSelectedIndex());
        
        // get xpath tu database
            Topic_DTO topic = new Topic_DTO();
            ArrayList<Topic_DTO> arr = new ArrayList<Topic_DTO>(); 
            Topic_BUS tp_bus = new Topic_BUS();
            arr = tp_bus.lay_xpath(idchuyenmuc);
            
            
            for (int i = 0; i < arr.size(); i++) {
                topic = arr.get(i);
            }
            
            dstin = laytin(topic.getUrl(), topic.get_link(),topic.get_title(), topic.get_img(), topic.get_time(), topic.get_content(), topic.get_next());
            
            // lay tin moi nhat trong database
            ArrayList<NoiDung_DTO> nddto = new ArrayList<>();
            NoiDung_BUS nd_bus = new NoiDung_BUS();
            
            nddto = nd_bus.LayNoiDung(idchuyenmuc);
            String tincuoi = nddto.get(0).getTen();
            
            // lay vi tri cua tin moi nhat
            int k = dstin.size()-1;
            for (int i = 0; i < dstin.size(); i++) {
                if (tincuoi.equals(dstin.get(i).getTen()) ) {
                   k = i;
                   break;
                }
            }
            k = k-1;
            // insert
            for(int i = k; i >= 0; i--)
            {
                NoiDung_BUS ndbus = new NoiDung_BUS();
                ndbus.Themtin(idchuyenmuc , dstin.get(i).getlink(), dstin.get(i).getTen(), dstin.get(i).getHinhanh(), dstin.get(i).getThoiGian(), dstin.get(i).getNoiDung());
            }
            
            JOptionPane.showMessageDialog(null, " tin mới được cập nhật thành công");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
            
            
    }//GEN-LAST:event_btn_laytinActionPerformed
    
    
    
    private void cb_tenSiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_tenSiteActionPerformed
        // TODO add your handling code here:
        
        
        matopic_tabhienthi = new Vector();

        int ma = (int) masite.get(cmb_Site.getSelectedIndex());

        Topic_BUS tpbus = new Topic_BUS();
        try {
            ArrayList<Topic_DTO> arr = tpbus.DanhSachTopic(ma);
            model = new DefaultComboBoxModel();
            for (int i = 0; i < arr.size(); i++) {
                model.addElement(arr.get(i).getTen());
                matopic_tabhienthi.addElement(arr.get(i).getId());
            }
            cb_tenTopic.setModel(model);

        } catch (Exception ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cb_tenSiteActionPerformed
   
    public JPanel Create_NoiDung(final int i)
    {      
        JPanel jp = new JPanel();
        jp.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2));
        jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));    
        jp.setPreferredSize(new Dimension(200,50));
        
       
        if(arr_noidung.get(i).getThoiGian() != null)
        {
            jp.add(new JLabel(arr_noidung.get(i).getThoiGian().toString()),JLabel.CENTER); 
        }
        
        jp.add(new JLabel(arr_noidung.get(i).getTen()),JLabel.CENTER);        
         
        if(arr_noidung.get(i).getHinhanh() != null)
        {
            BufferedImage bufferedImage = scaleImage(120, 80,arr_noidung.get(i).getHinhanh()); 
            jp.add(new JLabel(new ImageIcon(bufferedImage)));
        }
        JTextArea noidung = new JTextArea(arr_noidung.get(i).getNoiDung());
        noidung.setEditable(false);
        noidung.setLineWrap(true);
        noidung.setWrapStyleWord(true);
   
        JScrollPane spane = new JScrollPane(noidung);
        
        
        jp.add(spane);  
        
        JButton button = new JButton();        
        button.setIcon(new ImageIcon("wrong_red_icon.png"));
        button.addActionListener(new ActionListener() {
           
            public void actionPerformed(ActionEvent e) {
                try {
                    
                    NoiDung_BUS ndb = new NoiDung_BUS();
                    ndb.XoaNoiDung(arr_noidung.get(i).getId());                  
                    LoadNoiDung();
                   
                    } catch (Exception ex) {
                        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
        });
        jp.add(button);               
        
       
        return jp;
    }
    
    public void LoadWeb()
    {
        masite = new Vector();
         try {
            model = new DefaultComboBoxModel();
            Site_BUS stb = new Site_BUS();
            ArrayList<Site_DTO> arr = stb.DanhSachWeb();
            for(int i=0;i<arr.size();i++)
            {
                model.addElement(arr.get(i).getTen());
                masite.add(arr.get(i).getId());
                
            }
            cb_tenSite.setModel(model);
            cmb_Site.setModel(model);
        } catch (Exception ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void LoadTopic(int site)
    {
        model = new DefaultComboBoxModel();
        try 
        {
            Topic_BUS tpb = new Topic_BUS();
            arr_topic = tpb.DanhSachTopic(site);          
            for(int i=0;i<arr_topic.size();i++)
            {
                model.addElement(arr_topic.get(i).getTen());
            }
            cb_tenTopic.setModel(model);
        } 
        catch (Exception ex) 
        {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void LoadNoiDung() throws Exception
    {
        NoiDung_BUS ndb = new NoiDung_BUS();
        int index = cb_tenTopic.getSelectedIndex();

        arr_noidung = ndb.LayNoiDung(arr_topic.get(index).getId());
//         arr_noidung = ndb.LayNoiDung(3);
        jPanel3.setLayout(new GridLayout(1,2));
        jPanel3.removeAll();       
        for(int i=0;i<arr_noidung.size();i++)
        {                       
            jPanel3.add(Create_NoiDung(i));
        } 
        
        jPanel3.revalidate();
      
    }
    
    public BufferedImage scaleImage(int WIDTH, int HEIGHT, byte [] img) {
        BufferedImage bi = null;
        try {
            ImageIcon ii = new ImageIcon(img);//path to image
            bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = (Graphics2D) bi.createGraphics();
            g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
            g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return bi;
    }

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_laytin;
    private javax.swing.JButton btn_themchuyenmuc;
    private javax.swing.JButton btn_themsite;
    private javax.swing.JComboBox cb_tenSite;
    private javax.swing.JComboBox cb_tenTopic;
    private javax.swing.JComboBox cmb_Site;
    private javax.swing.JComboBox cmb_chuyenmuc;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField txt_Xpath_img;
    private javax.swing.JTextField txt_Xpath_link;
    private javax.swing.JTextField txt_Xpath_mota;
    private javax.swing.JTextField txt_Xpath_title;
    private javax.swing.JTextField txt_Xpath_trangtiep;
    private javax.swing.JTextField txt_time;
    private javax.swing.JTextField txt_weblink;
    // End of variables declaration//GEN-END:variables
}
